{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Chapter 5 - Grouping dataset.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OG5fpdcPAiJl",
        "colab_type": "text"
      },
      "source": [
        "**Group by Mechanics**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "26--0oNjAkS4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import pandas as pd "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "N5fGfZCu5jF6",
        "colab_type": "code",
        "outputId": "7142e3dc-389e-4835-bada-6ac650238eda",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 309
        }
      },
      "source": [
        "df = pd.read_csv(\"/content/automobileEDA.csv\")\n",
        "df.head()\n"
      ],
      "execution_count": 142,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>symboling</th>\n",
              "      <th>normalized-losses</th>\n",
              "      <th>make</th>\n",
              "      <th>fuel-type</th>\n",
              "      <th>aspiration</th>\n",
              "      <th>num-of-doors</th>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th>engine-location</th>\n",
              "      <th>wheel-base</th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>engine-type</th>\n",
              "      <th>num-of-cylinders</th>\n",
              "      <th>engine-size</th>\n",
              "      <th>fuel-system</th>\n",
              "      <th>bore</th>\n",
              "      <th>stroke</th>\n",
              "      <th>compression-ratio</th>\n",
              "      <th>horsepower</th>\n",
              "      <th>peak-rpm</th>\n",
              "      <th>city-mpg</th>\n",
              "      <th>highway-mpg</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>88.6</td>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>dohc</td>\n",
              "      <td>four</td>\n",
              "      <td>130</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.47</td>\n",
              "      <td>2.68</td>\n",
              "      <td>9.0</td>\n",
              "      <td>111</td>\n",
              "      <td>5000</td>\n",
              "      <td>21</td>\n",
              "      <td>27</td>\n",
              "      <td>13495</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>88.6</td>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>dohc</td>\n",
              "      <td>four</td>\n",
              "      <td>130</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.47</td>\n",
              "      <td>2.68</td>\n",
              "      <td>9.0</td>\n",
              "      <td>111</td>\n",
              "      <td>5000</td>\n",
              "      <td>21</td>\n",
              "      <td>27</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>1</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>hatchback</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>94.5</td>\n",
              "      <td>171.2</td>\n",
              "      <td>65.5</td>\n",
              "      <td>52.4</td>\n",
              "      <td>2823</td>\n",
              "      <td>ohcv</td>\n",
              "      <td>six</td>\n",
              "      <td>152</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>2.68</td>\n",
              "      <td>3.47</td>\n",
              "      <td>9.0</td>\n",
              "      <td>154</td>\n",
              "      <td>5000</td>\n",
              "      <td>19</td>\n",
              "      <td>26</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>2</td>\n",
              "      <td>164</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>sedan</td>\n",
              "      <td>fwd</td>\n",
              "      <td>front</td>\n",
              "      <td>99.8</td>\n",
              "      <td>176.6</td>\n",
              "      <td>66.2</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2337</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>109</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>10.0</td>\n",
              "      <td>102</td>\n",
              "      <td>5500</td>\n",
              "      <td>24</td>\n",
              "      <td>30</td>\n",
              "      <td>13950</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>2</td>\n",
              "      <td>164</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>sedan</td>\n",
              "      <td>4wd</td>\n",
              "      <td>front</td>\n",
              "      <td>99.4</td>\n",
              "      <td>176.6</td>\n",
              "      <td>66.4</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2824</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>136</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>8.0</td>\n",
              "      <td>115</td>\n",
              "      <td>5500</td>\n",
              "      <td>18</td>\n",
              "      <td>22</td>\n",
              "      <td>17450</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   symboling normalized-losses         make  ... city-mpg highway-mpg  price\n",
              "0          3                 ?  alfa-romero  ...       21          27  13495\n",
              "1          3                 ?  alfa-romero  ...       21          27  16500\n",
              "2          1                 ?  alfa-romero  ...       19          26  16500\n",
              "3          2               164         audi  ...       24          30  13950\n",
              "4          2               164         audi  ...       18          22  17450\n",
              "\n",
              "[5 rows x 26 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 142
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-8YOnIOv_oft",
        "colab_type": "text"
      },
      "source": [
        "As you can see there are multiple columns with categoical variable. Using groupby() function lets group these data set on the basis of body-style."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k08przPIl6vc",
        "colab_type": "code",
        "outputId": "9a3757cd-61c8-4e8e-fc57-a43e4d028b0f",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "#groups dataframe according to body-style and prints the keys of groups\n",
        "df.groupby('body-style').groups.keys()\n"
      ],
      "execution_count": 143,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dict_keys(['convertible', 'hardtop', 'hatchback', 'sedan', 'wagon'])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 143
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7B6dTiAO_310",
        "colab_type": "code",
        "outputId": "1b27cdd2-9b61-4772-d9ce-f11bbcca89b4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 306
        }
      },
      "source": [
        "style = df.groupby('body-style')\n",
        "#To print the values contained in group convertible\n",
        "style.get_group(\"convertible\")\n"
      ],
      "execution_count": 144,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>symboling</th>\n",
              "      <th>normalized-losses</th>\n",
              "      <th>make</th>\n",
              "      <th>fuel-type</th>\n",
              "      <th>aspiration</th>\n",
              "      <th>num-of-doors</th>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th>engine-location</th>\n",
              "      <th>wheel-base</th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>engine-type</th>\n",
              "      <th>num-of-cylinders</th>\n",
              "      <th>engine-size</th>\n",
              "      <th>fuel-system</th>\n",
              "      <th>bore</th>\n",
              "      <th>stroke</th>\n",
              "      <th>compression-ratio</th>\n",
              "      <th>horsepower</th>\n",
              "      <th>peak-rpm</th>\n",
              "      <th>city-mpg</th>\n",
              "      <th>highway-mpg</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>88.6</td>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>dohc</td>\n",
              "      <td>four</td>\n",
              "      <td>130</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.47</td>\n",
              "      <td>2.68</td>\n",
              "      <td>9.0</td>\n",
              "      <td>111</td>\n",
              "      <td>5000</td>\n",
              "      <td>21</td>\n",
              "      <td>27</td>\n",
              "      <td>13495</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>88.6</td>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>dohc</td>\n",
              "      <td>four</td>\n",
              "      <td>130</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.47</td>\n",
              "      <td>2.68</td>\n",
              "      <td>9.0</td>\n",
              "      <td>111</td>\n",
              "      <td>5000</td>\n",
              "      <td>21</td>\n",
              "      <td>27</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>72</th>\n",
              "      <td>3</td>\n",
              "      <td>142</td>\n",
              "      <td>mercedes-benz</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>96.6</td>\n",
              "      <td>180.3</td>\n",
              "      <td>70.5</td>\n",
              "      <td>50.8</td>\n",
              "      <td>3685</td>\n",
              "      <td>ohcv</td>\n",
              "      <td>eight</td>\n",
              "      <td>234</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.46</td>\n",
              "      <td>3.1</td>\n",
              "      <td>8.3</td>\n",
              "      <td>155</td>\n",
              "      <td>4750</td>\n",
              "      <td>16</td>\n",
              "      <td>18</td>\n",
              "      <td>35056</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>128</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>porsche</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>rear</td>\n",
              "      <td>89.5</td>\n",
              "      <td>168.9</td>\n",
              "      <td>65.0</td>\n",
              "      <td>51.6</td>\n",
              "      <td>2800</td>\n",
              "      <td>ohcf</td>\n",
              "      <td>six</td>\n",
              "      <td>194</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.74</td>\n",
              "      <td>2.9</td>\n",
              "      <td>9.5</td>\n",
              "      <td>207</td>\n",
              "      <td>5900</td>\n",
              "      <td>17</td>\n",
              "      <td>25</td>\n",
              "      <td>37028</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>172</th>\n",
              "      <td>2</td>\n",
              "      <td>134</td>\n",
              "      <td>toyota</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>front</td>\n",
              "      <td>98.4</td>\n",
              "      <td>176.2</td>\n",
              "      <td>65.6</td>\n",
              "      <td>53.0</td>\n",
              "      <td>2975</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>146</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.62</td>\n",
              "      <td>3.5</td>\n",
              "      <td>9.3</td>\n",
              "      <td>116</td>\n",
              "      <td>4800</td>\n",
              "      <td>24</td>\n",
              "      <td>30</td>\n",
              "      <td>17669</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>189</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>volkswagen</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>convertible</td>\n",
              "      <td>fwd</td>\n",
              "      <td>front</td>\n",
              "      <td>94.5</td>\n",
              "      <td>159.3</td>\n",
              "      <td>64.2</td>\n",
              "      <td>55.6</td>\n",
              "      <td>2254</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>109</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>8.5</td>\n",
              "      <td>90</td>\n",
              "      <td>5500</td>\n",
              "      <td>24</td>\n",
              "      <td>29</td>\n",
              "      <td>11595</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "     symboling normalized-losses           make  ... city-mpg highway-mpg  price\n",
              "0            3                 ?    alfa-romero  ...       21          27  13495\n",
              "1            3                 ?    alfa-romero  ...       21          27  16500\n",
              "72           3               142  mercedes-benz  ...       16          18  35056\n",
              "128          3                 ?        porsche  ...       17          25  37028\n",
              "172          2               134         toyota  ...       24          30  17669\n",
              "189          3                 ?     volkswagen  ...       24          29  11595\n",
              "\n",
              "[6 rows x 26 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 144
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iuXEL3HkAgef",
        "colab_type": "code",
        "outputId": "3157f8cb-6a55-4943-988c-09df71b304d5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 588
        }
      },
      "source": [
        "double_grouping = df.groupby([\"body-style\",\"drive-wheels\"])\n",
        "#To print the first values contained in each group\n",
        "double_grouping.first()"
      ],
      "execution_count": 145,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>symboling</th>\n",
              "      <th>normalized-losses</th>\n",
              "      <th>make</th>\n",
              "      <th>fuel-type</th>\n",
              "      <th>aspiration</th>\n",
              "      <th>num-of-doors</th>\n",
              "      <th>engine-location</th>\n",
              "      <th>wheel-base</th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>engine-type</th>\n",
              "      <th>num-of-cylinders</th>\n",
              "      <th>engine-size</th>\n",
              "      <th>fuel-system</th>\n",
              "      <th>bore</th>\n",
              "      <th>stroke</th>\n",
              "      <th>compression-ratio</th>\n",
              "      <th>horsepower</th>\n",
              "      <th>peak-rpm</th>\n",
              "      <th>city-mpg</th>\n",
              "      <th>highway-mpg</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>volkswagen</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>94.5</td>\n",
              "      <td>159.3</td>\n",
              "      <td>64.2</td>\n",
              "      <td>55.6</td>\n",
              "      <td>2254</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>109</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>8.5</td>\n",
              "      <td>90</td>\n",
              "      <td>5500</td>\n",
              "      <td>24</td>\n",
              "      <td>29</td>\n",
              "      <td>11595</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>3</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>88.6</td>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>dohc</td>\n",
              "      <td>four</td>\n",
              "      <td>130</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.47</td>\n",
              "      <td>2.68</td>\n",
              "      <td>9.0</td>\n",
              "      <td>111</td>\n",
              "      <td>5000</td>\n",
              "      <td>21</td>\n",
              "      <td>27</td>\n",
              "      <td>13495</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>2</td>\n",
              "      <td>168</td>\n",
              "      <td>nissan</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>95.1</td>\n",
              "      <td>162.4</td>\n",
              "      <td>63.8</td>\n",
              "      <td>53.3</td>\n",
              "      <td>2008</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>97</td>\n",
              "      <td>2bbl</td>\n",
              "      <td>3.15</td>\n",
              "      <td>3.29</td>\n",
              "      <td>9.4</td>\n",
              "      <td>69</td>\n",
              "      <td>5200</td>\n",
              "      <td>31</td>\n",
              "      <td>37</td>\n",
              "      <td>8249</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>0</td>\n",
              "      <td>93</td>\n",
              "      <td>mercedes-benz</td>\n",
              "      <td>diesel</td>\n",
              "      <td>turbo</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>106.7</td>\n",
              "      <td>187.5</td>\n",
              "      <td>70.3</td>\n",
              "      <td>54.9</td>\n",
              "      <td>3495</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>183</td>\n",
              "      <td>idi</td>\n",
              "      <td>3.58</td>\n",
              "      <td>3.64</td>\n",
              "      <td>21.5</td>\n",
              "      <td>123</td>\n",
              "      <td>4350</td>\n",
              "      <td>22</td>\n",
              "      <td>25</td>\n",
              "      <td>28176</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>0</td>\n",
              "      <td>?</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>turbo</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>99.5</td>\n",
              "      <td>178.2</td>\n",
              "      <td>67.9</td>\n",
              "      <td>52.0</td>\n",
              "      <td>3053</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>131</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.13</td>\n",
              "      <td>3.4</td>\n",
              "      <td>7.0</td>\n",
              "      <td>160</td>\n",
              "      <td>5500</td>\n",
              "      <td>16</td>\n",
              "      <td>22</td>\n",
              "      <td>?</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>2</td>\n",
              "      <td>121</td>\n",
              "      <td>chevrolet</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>88.4</td>\n",
              "      <td>141.1</td>\n",
              "      <td>60.3</td>\n",
              "      <td>53.2</td>\n",
              "      <td>1488</td>\n",
              "      <td>l</td>\n",
              "      <td>three</td>\n",
              "      <td>61</td>\n",
              "      <td>2bbl</td>\n",
              "      <td>2.91</td>\n",
              "      <td>3.03</td>\n",
              "      <td>9.5</td>\n",
              "      <td>48</td>\n",
              "      <td>5100</td>\n",
              "      <td>47</td>\n",
              "      <td>53</td>\n",
              "      <td>5151</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>1</td>\n",
              "      <td>?</td>\n",
              "      <td>alfa-romero</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>94.5</td>\n",
              "      <td>171.2</td>\n",
              "      <td>65.5</td>\n",
              "      <td>52.4</td>\n",
              "      <td>2823</td>\n",
              "      <td>ohcv</td>\n",
              "      <td>six</td>\n",
              "      <td>152</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>2.68</td>\n",
              "      <td>3.47</td>\n",
              "      <td>9.0</td>\n",
              "      <td>154</td>\n",
              "      <td>5000</td>\n",
              "      <td>19</td>\n",
              "      <td>26</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>2</td>\n",
              "      <td>164</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>front</td>\n",
              "      <td>99.4</td>\n",
              "      <td>176.6</td>\n",
              "      <td>66.4</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2824</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>136</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>8.0</td>\n",
              "      <td>115</td>\n",
              "      <td>5500</td>\n",
              "      <td>18</td>\n",
              "      <td>22</td>\n",
              "      <td>17450</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>2</td>\n",
              "      <td>164</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>front</td>\n",
              "      <td>99.8</td>\n",
              "      <td>176.6</td>\n",
              "      <td>66.2</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2337</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>109</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>10.0</td>\n",
              "      <td>102</td>\n",
              "      <td>5500</td>\n",
              "      <td>24</td>\n",
              "      <td>30</td>\n",
              "      <td>13950</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>2</td>\n",
              "      <td>192</td>\n",
              "      <td>bmw</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>two</td>\n",
              "      <td>front</td>\n",
              "      <td>101.2</td>\n",
              "      <td>176.8</td>\n",
              "      <td>64.8</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2395</td>\n",
              "      <td>ohc</td>\n",
              "      <td>four</td>\n",
              "      <td>108</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.5</td>\n",
              "      <td>2.8</td>\n",
              "      <td>8.8</td>\n",
              "      <td>101</td>\n",
              "      <td>5800</td>\n",
              "      <td>23</td>\n",
              "      <td>29</td>\n",
              "      <td>16430</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>0</td>\n",
              "      <td>85</td>\n",
              "      <td>subaru</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>front</td>\n",
              "      <td>96.9</td>\n",
              "      <td>173.6</td>\n",
              "      <td>65.4</td>\n",
              "      <td>54.9</td>\n",
              "      <td>2420</td>\n",
              "      <td>ohcf</td>\n",
              "      <td>four</td>\n",
              "      <td>108</td>\n",
              "      <td>2bbl</td>\n",
              "      <td>3.62</td>\n",
              "      <td>2.64</td>\n",
              "      <td>9.0</td>\n",
              "      <td>82</td>\n",
              "      <td>4800</td>\n",
              "      <td>23</td>\n",
              "      <td>29</td>\n",
              "      <td>8013</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>1</td>\n",
              "      <td>?</td>\n",
              "      <td>audi</td>\n",
              "      <td>gas</td>\n",
              "      <td>std</td>\n",
              "      <td>four</td>\n",
              "      <td>front</td>\n",
              "      <td>105.8</td>\n",
              "      <td>192.7</td>\n",
              "      <td>71.4</td>\n",
              "      <td>55.7</td>\n",
              "      <td>2954</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>136</td>\n",
              "      <td>mpfi</td>\n",
              "      <td>3.19</td>\n",
              "      <td>3.4</td>\n",
              "      <td>8.5</td>\n",
              "      <td>110</td>\n",
              "      <td>5500</td>\n",
              "      <td>19</td>\n",
              "      <td>25</td>\n",
              "      <td>18920</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>-1</td>\n",
              "      <td>93</td>\n",
              "      <td>mercedes-benz</td>\n",
              "      <td>diesel</td>\n",
              "      <td>turbo</td>\n",
              "      <td>four</td>\n",
              "      <td>front</td>\n",
              "      <td>110.0</td>\n",
              "      <td>190.9</td>\n",
              "      <td>70.3</td>\n",
              "      <td>58.7</td>\n",
              "      <td>3750</td>\n",
              "      <td>ohc</td>\n",
              "      <td>five</td>\n",
              "      <td>183</td>\n",
              "      <td>idi</td>\n",
              "      <td>3.58</td>\n",
              "      <td>3.64</td>\n",
              "      <td>21.5</td>\n",
              "      <td>123</td>\n",
              "      <td>4350</td>\n",
              "      <td>22</td>\n",
              "      <td>25</td>\n",
              "      <td>28248</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          symboling normalized-losses  ... highway-mpg  price\n",
              "body-style  drive-wheels                               ...                   \n",
              "convertible fwd                   3                 ?  ...          29  11595\n",
              "            rwd                   3                 ?  ...          27  13495\n",
              "hardtop     fwd                   2               168  ...          37   8249\n",
              "            rwd                   0                93  ...          25  28176\n",
              "hatchback   4wd                   0                 ?  ...          22      ?\n",
              "            fwd                   2               121  ...          53   5151\n",
              "            rwd                   1                 ?  ...          26  16500\n",
              "sedan       4wd                   2               164  ...          22  17450\n",
              "            fwd                   2               164  ...          30  13950\n",
              "            rwd                   2               192  ...          29  16430\n",
              "wagon       4wd                   0                85  ...          29   8013\n",
              "            fwd                   1                 ?  ...          25  18920\n",
              "            rwd                  -1                93  ...          25  28248\n",
              "\n",
              "[13 rows x 24 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 145
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OxIIBgi6rPFJ",
        "colab_type": "code",
        "outputId": "13d8e840-5399-4873-c2fd-1ee2b8387ce9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        }
      },
      "source": [
        "# max() will print the maximum entry of each group \n",
        "style['normalized-losses'].max()\n",
        "# min() will print the minimum entry of each group \n",
        "style['normalized-losses'].min()"
      ],
      "execution_count": 146,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "body-style\n",
              "convertible    134\n",
              "hardtop        134\n",
              "hatchback      101\n",
              "sedan          102\n",
              "wagon          103\n",
              "Name: normalized-losses, dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 146
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a7h7yA6tslIp",
        "colab_type": "code",
        "outputId": "5a4706aa-5e29-42cf-af30-2590340badbe",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        }
      },
      "source": [
        "\n",
        "# style['normalized-losses'].min() will print output in series \n",
        "style['normalized-losses'].min()\n",
        "\n",
        "# style[['normalized-losses']].min() will print output in data frame \n",
        "style[['normalized-losses']].min()"
      ],
      "execution_count": 147,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>normalized-losses</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>convertible</th>\n",
              "      <td>134</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hardtop</th>\n",
              "      <td>134</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hatchback</th>\n",
              "      <td>101</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sedan</th>\n",
              "      <td>102</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>wagon</th>\n",
              "      <td>103</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "            normalized-losses\n",
              "body-style                   \n",
              "convertible               134\n",
              "hardtop                   134\n",
              "hatchback                 101\n",
              "sedan                     102\n",
              "wagon                     103"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 147
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bioyqRDAeafj",
        "colab_type": "code",
        "outputId": "352f72b9-b856-43b0-ed99-c35186976155",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        }
      },
      "source": [
        "# mean() will print mean of numerical column in each group\n",
        "style.mean()"
      ],
      "execution_count": 148,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>symboling</th>\n",
              "      <th>wheel-base</th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>engine-size</th>\n",
              "      <th>compression-ratio</th>\n",
              "      <th>city-mpg</th>\n",
              "      <th>highway-mpg</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>convertible</th>\n",
              "      <td>2.833333</td>\n",
              "      <td>92.700000</td>\n",
              "      <td>170.383333</td>\n",
              "      <td>65.583333</td>\n",
              "      <td>51.433333</td>\n",
              "      <td>2801.666667</td>\n",
              "      <td>157.166667</td>\n",
              "      <td>8.933333</td>\n",
              "      <td>20.500000</td>\n",
              "      <td>26.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hardtop</th>\n",
              "      <td>1.875000</td>\n",
              "      <td>98.500000</td>\n",
              "      <td>176.937500</td>\n",
              "      <td>66.612500</td>\n",
              "      <td>52.850000</td>\n",
              "      <td>2810.625000</td>\n",
              "      <td>176.250000</td>\n",
              "      <td>10.725000</td>\n",
              "      <td>21.625000</td>\n",
              "      <td>27.250000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hatchback</th>\n",
              "      <td>1.585714</td>\n",
              "      <td>95.535714</td>\n",
              "      <td>166.592857</td>\n",
              "      <td>65.247143</td>\n",
              "      <td>52.108571</td>\n",
              "      <td>2348.185714</td>\n",
              "      <td>114.400000</td>\n",
              "      <td>9.027429</td>\n",
              "      <td>26.314286</td>\n",
              "      <td>32.171429</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sedan</th>\n",
              "      <td>0.333333</td>\n",
              "      <td>100.619792</td>\n",
              "      <td>177.585417</td>\n",
              "      <td>66.260417</td>\n",
              "      <td>54.337500</td>\n",
              "      <td>2610.593750</td>\n",
              "      <td>130.822917</td>\n",
              "      <td>10.937500</td>\n",
              "      <td>25.322917</td>\n",
              "      <td>30.833333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>wagon</th>\n",
              "      <td>-0.160000</td>\n",
              "      <td>102.156000</td>\n",
              "      <td>181.304000</td>\n",
              "      <td>66.256000</td>\n",
              "      <td>56.728000</td>\n",
              "      <td>2784.240000</td>\n",
              "      <td>123.840000</td>\n",
              "      <td>10.316000</td>\n",
              "      <td>24.040000</td>\n",
              "      <td>28.720000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "             symboling  wheel-base  ...   city-mpg  highway-mpg\n",
              "body-style                          ...                        \n",
              "convertible   2.833333   92.700000  ...  20.500000    26.000000\n",
              "hardtop       1.875000   98.500000  ...  21.625000    27.250000\n",
              "hatchback     1.585714   95.535714  ...  26.314286    32.171429\n",
              "sedan         0.333333  100.619792  ...  25.322917    30.833333\n",
              "wagon        -0.160000  102.156000  ...  24.040000    28.720000\n",
              "\n",
              "[5 rows x 10 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 148
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VhVzdvIbCqr9",
        "colab_type": "code",
        "outputId": "782b34d2-6986-4530-d1e7-4be9d83f0577",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 255
        }
      },
      "source": [
        "# get mean of each column of specific group\n",
        "style.get_group(\"convertible\").mean()"
      ],
      "execution_count": 149,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "symboling            2.833333e+00\n",
              "wheel-base           9.270000e+01\n",
              "length               1.703833e+02\n",
              "width                6.558333e+01\n",
              "height               5.143333e+01\n",
              "curb-weight          2.801667e+03\n",
              "engine-size          1.571667e+02\n",
              "compression-ratio    8.933333e+00\n",
              "horsepower           1.851853e+15\n",
              "peak-rpm             8.334167e+22\n",
              "city-mpg             2.050000e+01\n",
              "highway-mpg          2.600000e+01\n",
              "price                2.249194e+28\n",
              "dtype: float64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 149
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iRFJ4yX1iBHO",
        "colab_type": "code",
        "outputId": "ab1bded0-e47e-4a80-e83a-0a2f7ba1d6e1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        }
      },
      "source": [
        "# get the sum of the price for each body-style group\n",
        "style['price'].sum()"
      ],
      "execution_count": 150,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "body-style\n",
              "convertible                       134951650035056370281766911595\n",
              "hardtop                    2817645400824932528340288449963911199\n",
              "hatchback      16500?5151629555726377795762291296464796855539...\n",
              "sedan          1395017450152501771023875164301692520970211052...\n",
              "wagon          1892089217295282487349799914399124401386016695...\n",
              "Name: price, dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 150
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pPP2I2cxpeyt",
        "colab_type": "code",
        "outputId": "8c73b922-3f32-418f-d157-332aa3258e99",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        }
      },
      "source": [
        "# get the number of symboling/records in each group\n",
        "style['symboling'].count()"
      ],
      "execution_count": 151,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "body-style\n",
              "convertible     6\n",
              "hardtop         8\n",
              "hatchback      70\n",
              "sedan          96\n",
              "wagon          25\n",
              "Name: symboling, dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 151
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IXi2AxAh91lT",
        "colab_type": "text"
      },
      "source": [
        "# Merging on Index"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aNfmIWN_C8TW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from IPython.display import display_html\n",
        "\n",
        "def display_side_by_side(*args):\n",
        "    html_str=''\n",
        "    for df in args:\n",
        "        html_str+=df.to_html()\n",
        "    display_html(html_str.replace('table','table style=\"display:inline\"'),raw=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6MB-bv23908i",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 422
        },
        "outputId": "06495be8-d7ac-465d-dac6-7e6448d2d3b2"
      },
      "source": [
        "import pandas as pd\n",
        "\n",
        "left1 = pd.DataFrame({'key': ['apple','ball','apple', 'apple', 'ball', 'cat'], 'value': range(6)})\n",
        "right1 = pd.DataFrame({'group_val': [33.4, 5]}, index=['apple', 'ball'])\n",
        "display_side_by_side(left1,right1)\n",
        "\n",
        "df1 = pd.merge(left1, right1, left_on='key', right_index=True)\n",
        "df1"
      ],
      "execution_count": 153,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "<table style=\"display:inline\" border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>key</th>\n",
              "      <th>value</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>apple</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>ball</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>apple</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>apple</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>ball</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>cat</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table style=\"display:inline\"><table style=\"display:inline\" border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>group_val</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>apple</th>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>ball</th>\n",
              "      <td>5.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table style=\"display:inline\">"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>key</th>\n",
              "      <th>value</th>\n",
              "      <th>group_val</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>apple</td>\n",
              "      <td>0</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>apple</td>\n",
              "      <td>2</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>apple</td>\n",
              "      <td>3</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>ball</td>\n",
              "      <td>1</td>\n",
              "      <td>5.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>ball</td>\n",
              "      <td>4</td>\n",
              "      <td>5.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "     key  value  group_val\n",
              "0  apple      0       33.4\n",
              "2  apple      2       33.4\n",
              "3  apple      3       33.4\n",
              "1   ball      1        5.0\n",
              "4   ball      4        5.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 153
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "T0VK0WT1_SIV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "891cdd37-1350-4e89-9c12-97b484cd32aa"
      },
      "source": [
        "df2 = pd.merge(left1, right1, left_on='key', right_index=True, how='outer')\n",
        "df2"
      ],
      "execution_count": 154,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>key</th>\n",
              "      <th>value</th>\n",
              "      <th>group_val</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>apple</td>\n",
              "      <td>0</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>apple</td>\n",
              "      <td>2</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>apple</td>\n",
              "      <td>3</td>\n",
              "      <td>33.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>ball</td>\n",
              "      <td>1</td>\n",
              "      <td>5.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>ball</td>\n",
              "      <td>4</td>\n",
              "      <td>5.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>cat</td>\n",
              "      <td>5</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "     key  value  group_val\n",
              "0  apple      0       33.4\n",
              "2  apple      2       33.4\n",
              "3  apple      3       33.4\n",
              "1   ball      1        5.0\n",
              "4   ball      4        5.0\n",
              "5    cat      5        NaN"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 154
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "l_5ynRff_hQA",
        "colab_type": "text"
      },
      "source": [
        "# Concatenating along an axis"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VNB7UQwC_lu2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "d3b875c4-9e51-4931-eb7c-31bba513975c"
      },
      "source": [
        "import numpy as np\n",
        "\n",
        "yarra = np.arange(15).reshape((3,5))\n",
        "yarra"
      ],
      "execution_count": 155,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 0,  1,  2,  3,  4],\n",
              "       [ 5,  6,  7,  8,  9],\n",
              "       [10, 11, 12, 13, 14]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 155
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8kvKtMsu_6W7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "7b94339f-6e5b-42be-8fbc-1aba1e07a33c"
      },
      "source": [
        " np.concatenate([yarra, yarra], axis=1)"
      ],
      "execution_count": 156,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[ 0,  1,  2,  3,  4,  0,  1,  2,  3,  4],\n",
              "       [ 5,  6,  7,  8,  9,  5,  6,  7,  8,  9],\n",
              "       [10, 11, 12, 13, 14, 10, 11, 12, 13, 14]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 156
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zIOGvzcHwInp",
        "colab_type": "text"
      },
      "source": [
        "**Data Aggregation**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XRF96mU6SUpq",
        "colab_type": "code",
        "outputId": "8c77c0ef-1556-47fe-d6a3-47b4207e72a7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        }
      },
      "source": [
        "\n",
        "# new dataframe that consist length,width,height,curb-weight and price\n",
        "new_dataset = df.filter([\"length\",\"width\",\"height\",\"curb-weight\",\"price\"],axis=1)\n",
        "new_dataset"
      ],
      "execution_count": 157,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>13495</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>168.8</td>\n",
              "      <td>64.1</td>\n",
              "      <td>48.8</td>\n",
              "      <td>2548</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>171.2</td>\n",
              "      <td>65.5</td>\n",
              "      <td>52.4</td>\n",
              "      <td>2823</td>\n",
              "      <td>16500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>176.6</td>\n",
              "      <td>66.2</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2337</td>\n",
              "      <td>13950</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>176.6</td>\n",
              "      <td>66.4</td>\n",
              "      <td>54.3</td>\n",
              "      <td>2824</td>\n",
              "      <td>17450</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>200</th>\n",
              "      <td>188.8</td>\n",
              "      <td>68.9</td>\n",
              "      <td>55.5</td>\n",
              "      <td>2952</td>\n",
              "      <td>16845</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>201</th>\n",
              "      <td>188.8</td>\n",
              "      <td>68.8</td>\n",
              "      <td>55.5</td>\n",
              "      <td>3049</td>\n",
              "      <td>19045</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>202</th>\n",
              "      <td>188.8</td>\n",
              "      <td>68.9</td>\n",
              "      <td>55.5</td>\n",
              "      <td>3012</td>\n",
              "      <td>21485</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>203</th>\n",
              "      <td>188.8</td>\n",
              "      <td>68.9</td>\n",
              "      <td>55.5</td>\n",
              "      <td>3217</td>\n",
              "      <td>22470</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>204</th>\n",
              "      <td>188.8</td>\n",
              "      <td>68.9</td>\n",
              "      <td>55.5</td>\n",
              "      <td>3062</td>\n",
              "      <td>22625</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>205 rows × 5 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "     length  width  height  curb-weight  price\n",
              "0     168.8   64.1    48.8         2548  13495\n",
              "1     168.8   64.1    48.8         2548  16500\n",
              "2     171.2   65.5    52.4         2823  16500\n",
              "3     176.6   66.2    54.3         2337  13950\n",
              "4     176.6   66.4    54.3         2824  17450\n",
              "..      ...    ...     ...          ...    ...\n",
              "200   188.8   68.9    55.5         2952  16845\n",
              "201   188.8   68.8    55.5         3049  19045\n",
              "202   188.8   68.9    55.5         3012  21485\n",
              "203   188.8   68.9    55.5         3217  22470\n",
              "204   188.8   68.9    55.5         3062  22625\n",
              "\n",
              "[205 rows x 5 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 157
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aE8qFbkHVfeT",
        "colab_type": "code",
        "outputId": "26d20aea-0d28-41fc-8abd-86083730b7e0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        }
      },
      "source": [
        "\n",
        "# applying single aggregation for mean over the columns\n",
        "new_dataset.agg(\"mean\", axis=\"rows\")"
      ],
      "execution_count": 158,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "length          174.049268\n",
              "width            65.907805\n",
              "height           53.724878\n",
              "curb-weight    2555.565854\n",
              "dtype: float64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 158
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uEjlZbN6SsYo",
        "colab_type": "code",
        "outputId": "f986fdf6-441b-4369-d548-62d9f7fce7a3",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 111
        }
      },
      "source": [
        "# applying aggregation sum and minimun across all the columns \n",
        "new_dataset.agg(['sum', 'min'])  "
      ],
      "execution_count": 159,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>sum</th>\n",
              "      <td>35680.1</td>\n",
              "      <td>13511.1</td>\n",
              "      <td>11013.6</td>\n",
              "      <td>523891</td>\n",
              "      <td>134951650016500139501745015250177101892023875?...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>141.1</td>\n",
              "      <td>60.3</td>\n",
              "      <td>47.8</td>\n",
              "      <td>1488</td>\n",
              "      <td>10198</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "      length  ...                                              price\n",
              "sum  35680.1  ...  134951650016500139501745015250177101892023875?...\n",
              "min    141.1  ...                                              10198\n",
              "\n",
              "[2 rows x 5 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 159
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WGCG5zO4Suqf",
        "colab_type": "code",
        "outputId": "f3ec59cd-bed8-4164-fd51-ef7924a2f487",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 142
        }
      },
      "source": [
        "# find aggregation for these columns \n",
        "new_dataset.aggregate({\"length\":['sum', 'min'], \n",
        "              \"width\":['max', 'min'], \n",
        "              \"height\":['min', 'sum'],  \n",
        "              \"curb-weight\":['sum']}) \n",
        "# if any specific aggregation is not applied on a column\n",
        "# then it has NaN value corresponding to it"
      ],
      "execution_count": 160,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>length</th>\n",
              "      <th>width</th>\n",
              "      <th>height</th>\n",
              "      <th>curb-weight</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>max</th>\n",
              "      <td>NaN</td>\n",
              "      <td>72.3</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>141.1</td>\n",
              "      <td>60.3</td>\n",
              "      <td>47.8</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sum</th>\n",
              "      <td>35680.1</td>\n",
              "      <td>NaN</td>\n",
              "      <td>11013.6</td>\n",
              "      <td>523891.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "      length  width   height  curb-weight\n",
              "max      NaN   72.3      NaN          NaN\n",
              "min    141.1   60.3     47.8          NaN\n",
              "sum  35680.1    NaN  11013.6     523891.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 160
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KpgCjbfiMqw6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 476
        },
        "outputId": "79b1d702-575c-4a3f-cc9f-6e53e2dc25f8"
      },
      "source": [
        "df.dtypes"
      ],
      "execution_count": 161,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "symboling              int64\n",
              "normalized-losses     object\n",
              "make                  object\n",
              "fuel-type             object\n",
              "aspiration            object\n",
              "num-of-doors          object\n",
              "body-style            object\n",
              "drive-wheels          object\n",
              "engine-location       object\n",
              "wheel-base           float64\n",
              "length               float64\n",
              "width                float64\n",
              "height               float64\n",
              "curb-weight            int64\n",
              "engine-type           object\n",
              "num-of-cylinders      object\n",
              "engine-size            int64\n",
              "fuel-system           object\n",
              "bore                  object\n",
              "stroke                object\n",
              "compression-ratio    float64\n",
              "horsepower            object\n",
              "peak-rpm              object\n",
              "city-mpg               int64\n",
              "highway-mpg            int64\n",
              "price                 object\n",
              "dtype: object"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 161
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sUE2Ml9uKD3s",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        },
        "outputId": "44afde78-3367-4c32-b1e9-f5e20e375c11"
      },
      "source": [
        "df['price'].str.isnumeric().value_counts()\n",
        "df['price'].loc[df['price'].str.isnumeric() == False]\n",
        "price = df['price'].loc[df['price'] != '?']\n",
        "pmean = price.astype(str).astype(int).mean()\n",
        "df['price'] = df['price'].replace('?',pmean).astype(int)\n",
        "\n",
        "df.groupby([\"body-style\",\"drive-wheels\"]).agg({'height': min, 'length': max, 'price': 'mean'})"
      ],
      "execution_count": 162,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>height</th>\n",
              "      <th>length</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>55.6</td>\n",
              "      <td>159.3</td>\n",
              "      <td>11595.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>48.8</td>\n",
              "      <td>180.3</td>\n",
              "      <td>23949.600000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>53.3</td>\n",
              "      <td>162.4</td>\n",
              "      <td>8249.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>51.6</td>\n",
              "      <td>199.2</td>\n",
              "      <td>24202.714286</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>52.0</td>\n",
              "      <td>178.2</td>\n",
              "      <td>10405.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>49.4</td>\n",
              "      <td>186.6</td>\n",
              "      <td>8396.387755</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>49.6</td>\n",
              "      <td>183.5</td>\n",
              "      <td>14278.263158</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.3</td>\n",
              "      <td>176.6</td>\n",
              "      <td>12647.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>50.6</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9930.929825</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>47.8</td>\n",
              "      <td>208.1</td>\n",
              "      <td>21711.833333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.9</td>\n",
              "      <td>173.6</td>\n",
              "      <td>9095.750000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>53.0</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9997.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>54.1</td>\n",
              "      <td>198.9</td>\n",
              "      <td>16994.222222</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          height  length         price\n",
              "body-style  drive-wheels                              \n",
              "convertible fwd             55.6   159.3  11595.000000\n",
              "            rwd             48.8   180.3  23949.600000\n",
              "hardtop     fwd             53.3   162.4   8249.000000\n",
              "            rwd             51.6   199.2  24202.714286\n",
              "hatchback   4wd             52.0   178.2  10405.000000\n",
              "            fwd             49.4   186.6   8396.387755\n",
              "            rwd             49.6   183.5  14278.263158\n",
              "sedan       4wd             54.3   176.6  12647.333333\n",
              "            fwd             50.6   192.7   9930.929825\n",
              "            rwd             47.8   208.1  21711.833333\n",
              "wagon       4wd             54.9   173.6   9095.750000\n",
              "            fwd             53.0   192.7   9997.333333\n",
              "            rwd             54.1   198.9  16994.222222"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 162
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uGkAVxmiY8sW",
        "colab_type": "text"
      },
      "source": [
        "**Group-wise operations**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jRV1MuUEV4FP",
        "colab_type": "code",
        "outputId": "81207b3f-da67-4bf7-b2d2-8c82f0a462ed",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        }
      },
      "source": [
        "\n",
        "# Group the data frame df by body-style and drive-wheels and extract stats from each group\n",
        "df.groupby(\n",
        "   [\"body-style\",\"drive-wheels\"]\n",
        ").agg(\n",
        "    {\n",
        "         'height': min,    # minimum height of car in each group\n",
        "         'length': max,  # maximum length of car in each group\n",
        "         'price': 'mean',  # average price of car in each group\n",
        "        \n",
        "    }\n",
        ")"
      ],
      "execution_count": 163,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>height</th>\n",
              "      <th>length</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>55.6</td>\n",
              "      <td>159.3</td>\n",
              "      <td>11595.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>48.8</td>\n",
              "      <td>180.3</td>\n",
              "      <td>23949.600000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>53.3</td>\n",
              "      <td>162.4</td>\n",
              "      <td>8249.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>51.6</td>\n",
              "      <td>199.2</td>\n",
              "      <td>24202.714286</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>52.0</td>\n",
              "      <td>178.2</td>\n",
              "      <td>10405.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>49.4</td>\n",
              "      <td>186.6</td>\n",
              "      <td>8396.387755</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>49.6</td>\n",
              "      <td>183.5</td>\n",
              "      <td>14278.263158</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.3</td>\n",
              "      <td>176.6</td>\n",
              "      <td>12647.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>50.6</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9930.929825</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>47.8</td>\n",
              "      <td>208.1</td>\n",
              "      <td>21711.833333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.9</td>\n",
              "      <td>173.6</td>\n",
              "      <td>9095.750000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>53.0</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9997.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>54.1</td>\n",
              "      <td>198.9</td>\n",
              "      <td>16994.222222</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          height  length         price\n",
              "body-style  drive-wheels                              \n",
              "convertible fwd             55.6   159.3  11595.000000\n",
              "            rwd             48.8   180.3  23949.600000\n",
              "hardtop     fwd             53.3   162.4   8249.000000\n",
              "            rwd             51.6   199.2  24202.714286\n",
              "hatchback   4wd             52.0   178.2  10405.000000\n",
              "            fwd             49.4   186.6   8396.387755\n",
              "            rwd             49.6   183.5  14278.263158\n",
              "sedan       4wd             54.3   176.6  12647.333333\n",
              "            fwd             50.6   192.7   9930.929825\n",
              "            rwd             47.8   208.1  21711.833333\n",
              "wagon       4wd             54.9   173.6   9095.750000\n",
              "            fwd             53.0   192.7   9997.333333\n",
              "            rwd             54.1   198.9  16994.222222"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 163
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0Z1ayb-pbK5M",
        "colab_type": "code",
        "outputId": "7c06f657-488c-434b-ae25-ef3e34e2d0c4",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        }
      },
      "source": [
        "# create dictionary of aggregrations\n",
        "aggregations=(\n",
        "    {\n",
        "         'height':min,    # minimum height of car in each group\n",
        "         'length': max,  # maximum length of car in each group\n",
        "         'price': 'mean',  # average price of car in each group\n",
        "        \n",
        "    }\n",
        ")\n",
        "# implementing aggregations in groups\n",
        "df.groupby(\n",
        "   [\"body-style\",\"drive-wheels\"]\n",
        ").agg(aggregations) \n"
      ],
      "execution_count": 164,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>height</th>\n",
              "      <th>length</th>\n",
              "      <th>price</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>55.6</td>\n",
              "      <td>159.3</td>\n",
              "      <td>11595.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>48.8</td>\n",
              "      <td>180.3</td>\n",
              "      <td>23949.600000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>53.3</td>\n",
              "      <td>162.4</td>\n",
              "      <td>8249.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>51.6</td>\n",
              "      <td>199.2</td>\n",
              "      <td>24202.714286</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>52.0</td>\n",
              "      <td>178.2</td>\n",
              "      <td>10405.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>49.4</td>\n",
              "      <td>186.6</td>\n",
              "      <td>8396.387755</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>49.6</td>\n",
              "      <td>183.5</td>\n",
              "      <td>14278.263158</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.3</td>\n",
              "      <td>176.6</td>\n",
              "      <td>12647.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>50.6</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9930.929825</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>47.8</td>\n",
              "      <td>208.1</td>\n",
              "      <td>21711.833333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.9</td>\n",
              "      <td>173.6</td>\n",
              "      <td>9095.750000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>53.0</td>\n",
              "      <td>192.7</td>\n",
              "      <td>9997.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>54.1</td>\n",
              "      <td>198.9</td>\n",
              "      <td>16994.222222</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          height  length         price\n",
              "body-style  drive-wheels                              \n",
              "convertible fwd             55.6   159.3  11595.000000\n",
              "            rwd             48.8   180.3  23949.600000\n",
              "hardtop     fwd             53.3   162.4   8249.000000\n",
              "            rwd             51.6   199.2  24202.714286\n",
              "hatchback   4wd             52.0   178.2  10405.000000\n",
              "            fwd             49.4   186.6   8396.387755\n",
              "            rwd             49.6   183.5  14278.263158\n",
              "sedan       4wd             54.3   176.6  12647.333333\n",
              "            fwd             50.6   192.7   9930.929825\n",
              "            rwd             47.8   208.1  21711.833333\n",
              "wagon       4wd             54.9   173.6   9095.750000\n",
              "            fwd             53.0   192.7   9997.333333\n",
              "            rwd             54.1   198.9  16994.222222"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 164
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nCKDPiUsi0hr",
        "colab_type": "text"
      },
      "source": [
        "We can use numpy functions in aggregation as well"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K5hrlkZOjApd",
        "colab_type": "code",
        "outputId": "9306c089-4c25-4a18-fb8a-b451b19b9bc2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        }
      },
      "source": [
        "# import the numpy library as np\n",
        "import numpy as np\n",
        "# using numpy libraries for operations\n",
        "df.groupby(\n",
        "   [\"body-style\",\"drive-wheels\"])[\"price\"].agg([np.sum, np.mean, np.std])"
      ],
      "execution_count": 165,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>sum</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>11595</td>\n",
              "      <td>11595.000000</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>119748</td>\n",
              "      <td>23949.600000</td>\n",
              "      <td>11165.099700</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>8249</td>\n",
              "      <td>8249.000000</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>169419</td>\n",
              "      <td>24202.714286</td>\n",
              "      <td>14493.311190</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>20810</td>\n",
              "      <td>10405.000000</td>\n",
              "      <td>3962.626402</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>411423</td>\n",
              "      <td>8396.387755</td>\n",
              "      <td>3004.675695</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>271287</td>\n",
              "      <td>14278.263158</td>\n",
              "      <td>3732.860727</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>37942</td>\n",
              "      <td>12647.333333</td>\n",
              "      <td>4280.814681</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>566063</td>\n",
              "      <td>9930.929825</td>\n",
              "      <td>3513.098067</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>781626</td>\n",
              "      <td>21711.833333</td>\n",
              "      <td>9194.820239</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>36383</td>\n",
              "      <td>9095.750000</td>\n",
              "      <td>1775.652063</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>119968</td>\n",
              "      <td>9997.333333</td>\n",
              "      <td>3584.185551</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>152948</td>\n",
              "      <td>16994.222222</td>\n",
              "      <td>4686.703313</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                             sum          mean           std\n",
              "body-style  drive-wheels                                    \n",
              "convertible fwd            11595  11595.000000           NaN\n",
              "            rwd           119748  23949.600000  11165.099700\n",
              "hardtop     fwd             8249   8249.000000           NaN\n",
              "            rwd           169419  24202.714286  14493.311190\n",
              "hatchback   4wd            20810  10405.000000   3962.626402\n",
              "            fwd           411423   8396.387755   3004.675695\n",
              "            rwd           271287  14278.263158   3732.860727\n",
              "sedan       4wd            37942  12647.333333   4280.814681\n",
              "            fwd           566063   9930.929825   3513.098067\n",
              "            rwd           781626  21711.833333   9194.820239\n",
              "wagon       4wd            36383   9095.750000   1775.652063\n",
              "            fwd           119968   9997.333333   3584.185551\n",
              "            rwd           152948  16994.222222   4686.703313"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 165
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6kshOptBfu5H",
        "colab_type": "text"
      },
      "source": [
        "**Renaming grouped aggregation columns**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OjhK-4xAgXiK",
        "colab_type": "code",
        "outputId": "f1fe13fa-d536-496e-f421-45c9b2608f11",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        }
      },
      "source": [
        "df.groupby(\n",
        "   [\"body-style\",\"drive-wheels\"]).agg(\n",
        "    # Get max of the price column for each group\n",
        "    max_price=('price', max),\n",
        "    # Get min of the price column for each group\n",
        "    min_price=('price', min),\n",
        "    # Get sum of the price column for each group\n",
        "    total_price=('price', 'mean')\n",
        "      \n",
        ")"
      ],
      "execution_count": 166,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>max_price</th>\n",
              "      <th>min_price</th>\n",
              "      <th>total_price</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>11595</td>\n",
              "      <td>11595</td>\n",
              "      <td>11595.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>37028</td>\n",
              "      <td>13495</td>\n",
              "      <td>23949.600000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>8249</td>\n",
              "      <td>8249</td>\n",
              "      <td>8249.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>45400</td>\n",
              "      <td>8449</td>\n",
              "      <td>24202.714286</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>13207</td>\n",
              "      <td>7603</td>\n",
              "      <td>10405.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>18150</td>\n",
              "      <td>5118</td>\n",
              "      <td>8396.387755</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>22018</td>\n",
              "      <td>8238</td>\n",
              "      <td>14278.263158</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>17450</td>\n",
              "      <td>9233</td>\n",
              "      <td>12647.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>23875</td>\n",
              "      <td>5499</td>\n",
              "      <td>9930.929825</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>41315</td>\n",
              "      <td>6785</td>\n",
              "      <td>21711.833333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>11694</td>\n",
              "      <td>7898</td>\n",
              "      <td>9095.750000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>18920</td>\n",
              "      <td>6918</td>\n",
              "      <td>9997.333333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>28248</td>\n",
              "      <td>12440</td>\n",
              "      <td>16994.222222</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          max_price  min_price   total_price\n",
              "body-style  drive-wheels                                    \n",
              "convertible fwd               11595      11595  11595.000000\n",
              "            rwd               37028      13495  23949.600000\n",
              "hardtop     fwd                8249       8249   8249.000000\n",
              "            rwd               45400       8449  24202.714286\n",
              "hatchback   4wd               13207       7603  10405.000000\n",
              "            fwd               18150       5118   8396.387755\n",
              "            rwd               22018       8238  14278.263158\n",
              "sedan       4wd               17450       9233  12647.333333\n",
              "            fwd               23875       5499   9930.929825\n",
              "            rwd               41315       6785  21711.833333\n",
              "wagon       4wd               11694       7898   9095.750000\n",
              "            fwd               18920       6918   9997.333333\n",
              "            rwd               28248      12440  16994.222222"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 166
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "V9Dze7S1kS8E",
        "colab_type": "text"
      },
      "source": [
        "**Group-wise transformations**\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ya68XoY4hMDG",
        "colab_type": "code",
        "outputId": "d5146eb7-d52d-4678-b109-fcb956e2fc15",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "source": [
        "df[\"price\"]=df[\"price\"].transform(lambda x:x + x/10)\n",
        "df.loc[:,'price']"
      ],
      "execution_count": 167,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0      14844.5\n",
              "1      18150.0\n",
              "2      18150.0\n",
              "3      15345.0\n",
              "4      19195.0\n",
              "        ...   \n",
              "200    18529.5\n",
              "201    20949.5\n",
              "202    23633.5\n",
              "203    24717.0\n",
              "204    24887.5\n",
              "Name: price, Length: 205, dtype: float64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 167
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "d2latPv6gEQh",
        "colab_type": "text"
      },
      "source": [
        "Let's observe average price of cars for each grouping by body-style and drive-wheels"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZDJI-vaLgVFt",
        "colab_type": "code",
        "outputId": "b6f6d0bb-d8cb-4e9b-d2aa-0df54685a2f8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        }
      },
      "source": [
        "df.groupby([\"body-style\",\"drive-wheels\"])[\"price\"].transform('mean')"
      ],
      "execution_count": 168,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0      26344.560000\n",
              "1      26344.560000\n",
              "2      15706.089474\n",
              "3      10924.022807\n",
              "4      13912.066667\n",
              "           ...     \n",
              "200    23883.016667\n",
              "201    23883.016667\n",
              "202    23883.016667\n",
              "203    23883.016667\n",
              "204    23883.016667\n",
              "Name: price, Length: 205, dtype: float64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 168
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3mfWxmmCfZAc",
        "colab_type": "code",
        "outputId": "459a9ecc-9460-41a3-c553-ed5e1cf367e0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 419
        }
      },
      "source": [
        "df[\"average-price\"]=df.groupby([\"body-style\",\"drive-wheels\"])[\"price\"].transform('mean')\n",
        "# selectiing columns body-style,drive-wheels,price and average-price\n",
        "df.loc[:,[\"body-style\",\"drive-wheels\",\"price\",\"average-price\"]]"
      ],
      "execution_count": 169,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th>price</th>\n",
              "      <th>average-price</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>14844.5</td>\n",
              "      <td>26344.560000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>convertible</td>\n",
              "      <td>rwd</td>\n",
              "      <td>18150.0</td>\n",
              "      <td>26344.560000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>hatchback</td>\n",
              "      <td>rwd</td>\n",
              "      <td>18150.0</td>\n",
              "      <td>15706.089474</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>sedan</td>\n",
              "      <td>fwd</td>\n",
              "      <td>15345.0</td>\n",
              "      <td>10924.022807</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>sedan</td>\n",
              "      <td>4wd</td>\n",
              "      <td>19195.0</td>\n",
              "      <td>13912.066667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>200</th>\n",
              "      <td>sedan</td>\n",
              "      <td>rwd</td>\n",
              "      <td>18529.5</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>201</th>\n",
              "      <td>sedan</td>\n",
              "      <td>rwd</td>\n",
              "      <td>20949.5</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>202</th>\n",
              "      <td>sedan</td>\n",
              "      <td>rwd</td>\n",
              "      <td>23633.5</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>203</th>\n",
              "      <td>sedan</td>\n",
              "      <td>rwd</td>\n",
              "      <td>24717.0</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>204</th>\n",
              "      <td>sedan</td>\n",
              "      <td>rwd</td>\n",
              "      <td>24887.5</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>205 rows × 4 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      body-style drive-wheels    price  average-price\n",
              "0    convertible          rwd  14844.5   26344.560000\n",
              "1    convertible          rwd  18150.0   26344.560000\n",
              "2      hatchback          rwd  18150.0   15706.089474\n",
              "3          sedan          fwd  15345.0   10924.022807\n",
              "4          sedan          4wd  19195.0   13912.066667\n",
              "..           ...          ...      ...            ...\n",
              "200        sedan          rwd  18529.5   23883.016667\n",
              "201        sedan          rwd  20949.5   23883.016667\n",
              "202        sedan          rwd  23633.5   23883.016667\n",
              "203        sedan          rwd  24717.0   23883.016667\n",
              "204        sedan          rwd  24887.5   23883.016667\n",
              "\n",
              "[205 rows x 4 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 169
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Er-gWccfpiM7",
        "colab_type": "text"
      },
      "source": [
        "**Pivot Tables and Cross-Tabulations**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W8bJS0rdIP1P",
        "colab_type": "code",
        "outputId": "3d8292e2-fc3f-443e-ae5d-f5bb20893acc",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        }
      },
      "source": [
        "new_dataset1 = df.filter([\"body-style\",\"drive-wheels\",\n",
        "                          \"length\",\"width\",\"height\",\"curb-weight\",\"price\"],axis=1)\n",
        "#simplest pivot table with dataframe df and index body-style\n",
        "table = pd.pivot_table(new_dataset1, index =[\"body-style\"]) \n",
        "table"
      ],
      "execution_count": 170,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>height</th>\n",
              "      <th>length</th>\n",
              "      <th>price</th>\n",
              "      <th>width</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>convertible</th>\n",
              "      <td>2801.666667</td>\n",
              "      <td>51.433333</td>\n",
              "      <td>170.383333</td>\n",
              "      <td>24079.550000</td>\n",
              "      <td>65.583333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hardtop</th>\n",
              "      <td>2810.625000</td>\n",
              "      <td>52.850000</td>\n",
              "      <td>176.937500</td>\n",
              "      <td>24429.350000</td>\n",
              "      <td>66.612500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hatchback</th>\n",
              "      <td>2348.185714</td>\n",
              "      <td>52.108571</td>\n",
              "      <td>166.592857</td>\n",
              "      <td>11055.314286</td>\n",
              "      <td>65.247143</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sedan</th>\n",
              "      <td>2610.593750</td>\n",
              "      <td>54.337500</td>\n",
              "      <td>177.585417</td>\n",
              "      <td>15877.021875</td>\n",
              "      <td>66.260417</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>wagon</th>\n",
              "      <td>2784.240000</td>\n",
              "      <td>56.728000</td>\n",
              "      <td>181.304000</td>\n",
              "      <td>13609.156000</td>\n",
              "      <td>66.256000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "             curb-weight     height      length         price      width\n",
              "body-style                                                              \n",
              "convertible  2801.666667  51.433333  170.383333  24079.550000  65.583333\n",
              "hardtop      2810.625000  52.850000  176.937500  24429.350000  66.612500\n",
              "hatchback    2348.185714  52.108571  166.592857  11055.314286  65.247143\n",
              "sedan        2610.593750  54.337500  177.585417  15877.021875  66.260417\n",
              "wagon        2784.240000  56.728000  181.304000  13609.156000  66.256000"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 170
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lqvI6BNYXUg5",
        "colab_type": "code",
        "outputId": "6c6f863a-7d60-4f64-83db-da91a28f7a45",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 483
        }
      },
      "source": [
        "#pivot table with dataframe df and index body-style and drive-wheels\n",
        "table = pd.pivot_table(new_dataset1, index =[\"body-style\",\"drive-wheels\"]) \n",
        "table"
      ],
      "execution_count": 171,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>curb-weight</th>\n",
              "      <th>height</th>\n",
              "      <th>length</th>\n",
              "      <th>price</th>\n",
              "      <th>width</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>2254.000000</td>\n",
              "      <td>55.600000</td>\n",
              "      <td>159.300000</td>\n",
              "      <td>12754.500000</td>\n",
              "      <td>64.200000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>2911.200000</td>\n",
              "      <td>50.600000</td>\n",
              "      <td>172.600000</td>\n",
              "      <td>26344.560000</td>\n",
              "      <td>65.860000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>2008.000000</td>\n",
              "      <td>53.300000</td>\n",
              "      <td>162.400000</td>\n",
              "      <td>9073.900000</td>\n",
              "      <td>63.800000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>2925.285714</td>\n",
              "      <td>52.785714</td>\n",
              "      <td>179.014286</td>\n",
              "      <td>26622.985714</td>\n",
              "      <td>67.014286</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>2646.500000</td>\n",
              "      <td>53.850000</td>\n",
              "      <td>167.750000</td>\n",
              "      <td>11445.500000</td>\n",
              "      <td>65.850000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>2181.551020</td>\n",
              "      <td>52.442857</td>\n",
              "      <td>163.944898</td>\n",
              "      <td>9236.026531</td>\n",
              "      <td>64.671429</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>2746.526316</td>\n",
              "      <td>51.063158</td>\n",
              "      <td>173.300000</td>\n",
              "      <td>15706.089474</td>\n",
              "      <td>66.668421</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>2573.000000</td>\n",
              "      <td>54.300000</td>\n",
              "      <td>173.533333</td>\n",
              "      <td>13912.066667</td>\n",
              "      <td>65.733333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>2298.228070</td>\n",
              "      <td>53.887719</td>\n",
              "      <td>171.812281</td>\n",
              "      <td>10924.022807</td>\n",
              "      <td>65.326316</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>3108.305556</td>\n",
              "      <td>55.052778</td>\n",
              "      <td>187.063889</td>\n",
              "      <td>23883.016667</td>\n",
              "      <td>67.783333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>2617.500000</td>\n",
              "      <td>57.000000</td>\n",
              "      <td>171.650000</td>\n",
              "      <td>10005.325000</td>\n",
              "      <td>64.500000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>2464.333333</td>\n",
              "      <td>56.008333</td>\n",
              "      <td>175.441667</td>\n",
              "      <td>10997.066667</td>\n",
              "      <td>65.533333</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>3284.888889</td>\n",
              "      <td>57.566667</td>\n",
              "      <td>193.411111</td>\n",
              "      <td>18693.644444</td>\n",
              "      <td>68.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                          curb-weight     height  ...         price      width\n",
              "body-style  drive-wheels                          ...                         \n",
              "convertible fwd           2254.000000  55.600000  ...  12754.500000  64.200000\n",
              "            rwd           2911.200000  50.600000  ...  26344.560000  65.860000\n",
              "hardtop     fwd           2008.000000  53.300000  ...   9073.900000  63.800000\n",
              "            rwd           2925.285714  52.785714  ...  26622.985714  67.014286\n",
              "hatchback   4wd           2646.500000  53.850000  ...  11445.500000  65.850000\n",
              "            fwd           2181.551020  52.442857  ...   9236.026531  64.671429\n",
              "            rwd           2746.526316  51.063158  ...  15706.089474  66.668421\n",
              "sedan       4wd           2573.000000  54.300000  ...  13912.066667  65.733333\n",
              "            fwd           2298.228070  53.887719  ...  10924.022807  65.326316\n",
              "            rwd           3108.305556  55.052778  ...  23883.016667  67.783333\n",
              "wagon       4wd           2617.500000  57.000000  ...  10005.325000  64.500000\n",
              "            fwd           2464.333333  56.008333  ...  10997.066667  65.533333\n",
              "            rwd           3284.888889  57.566667  ...  18693.644444  68.000000\n",
              "\n",
              "[13 rows x 5 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 171
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cgxYlOqiS-R6",
        "colab_type": "code",
        "outputId": "2f16cb51-400f-41ed-afab-b76477ad3430",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        }
      },
      "source": [
        "# import numpy for aggregration function\n",
        "import numpy as np\n",
        "\n",
        "#new data set with few columns\n",
        "new_dataset3 = df.filter([\"body-style\",\"drive-wheels\",\"price\"],axis=1)\n",
        "\n",
        "# pivot table with dataset new_dataset2\n",
        "# values are the column in which aggregration function is to be applied\n",
        "# index is column for grouping of data\n",
        "# columns for specifying category of data \n",
        "# aggfunc is the aggregration function to be applied\n",
        "# fill_value to fill missing values\n",
        "table = pd.pivot_table(new_dataset3, values='price', index=[\"body-style\"],\n",
        "                       columns=[\"drive-wheels\"],aggfunc=np.mean,fill_value=0)\n",
        "table"
      ],
      "execution_count": 172,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>drive-wheels</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>convertible</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>12754.500000</td>\n",
              "      <td>26344.560000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hardtop</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>9073.900000</td>\n",
              "      <td>26622.985714</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>hatchback</th>\n",
              "      <td>11445.500000</td>\n",
              "      <td>9236.026531</td>\n",
              "      <td>15706.089474</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sedan</th>\n",
              "      <td>13912.066667</td>\n",
              "      <td>10924.022807</td>\n",
              "      <td>23883.016667</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>wagon</th>\n",
              "      <td>10005.325000</td>\n",
              "      <td>10997.066667</td>\n",
              "      <td>18693.644444</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "drive-wheels           4wd           fwd           rwd\n",
              "body-style                                            \n",
              "convertible       0.000000  12754.500000  26344.560000\n",
              "hardtop           0.000000   9073.900000  26622.985714\n",
              "hatchback     11445.500000   9236.026531  15706.089474\n",
              "sedan         13912.066667  10924.022807  23883.016667\n",
              "wagon         10005.325000  10997.066667  18693.644444"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 172
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gyZ2dsuEZPsv",
        "colab_type": "code",
        "outputId": "8534f49b-834a-446e-c6f3-95041f2d7d3a",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 514
        }
      },
      "source": [
        "table = pd.pivot_table(new_dataset1, values=['price','height','width'],\n",
        "                       index =[\"body-style\",\"drive-wheels\"],\n",
        "                       aggfunc={'price': np.mean,'height': [min, max],'width': [min, max]},\n",
        "                       fill_value=0)\n",
        "table"
      ],
      "execution_count": 173,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr th {\n",
              "        text-align: left;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr:last-of-type th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th colspan=\"2\" halign=\"left\">height</th>\n",
              "      <th>price</th>\n",
              "      <th colspan=\"2\" halign=\"left\">width</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>max</th>\n",
              "      <th>min</th>\n",
              "      <th>mean</th>\n",
              "      <th>max</th>\n",
              "      <th>min</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>body-style</th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">convertible</th>\n",
              "      <th>fwd</th>\n",
              "      <td>55.6</td>\n",
              "      <td>55.6</td>\n",
              "      <td>12754.500000</td>\n",
              "      <td>64.2</td>\n",
              "      <td>64.2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>53.0</td>\n",
              "      <td>48.8</td>\n",
              "      <td>26344.560000</td>\n",
              "      <td>70.5</td>\n",
              "      <td>64.1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">hardtop</th>\n",
              "      <th>fwd</th>\n",
              "      <td>53.3</td>\n",
              "      <td>53.3</td>\n",
              "      <td>9073.900000</td>\n",
              "      <td>63.8</td>\n",
              "      <td>63.8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>55.4</td>\n",
              "      <td>51.6</td>\n",
              "      <td>26622.985714</td>\n",
              "      <td>72.0</td>\n",
              "      <td>65.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">hatchback</th>\n",
              "      <th>4wd</th>\n",
              "      <td>55.7</td>\n",
              "      <td>52.0</td>\n",
              "      <td>11445.500000</td>\n",
              "      <td>67.9</td>\n",
              "      <td>63.8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>56.1</td>\n",
              "      <td>49.4</td>\n",
              "      <td>9236.026531</td>\n",
              "      <td>66.6</td>\n",
              "      <td>60.3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>54.8</td>\n",
              "      <td>49.6</td>\n",
              "      <td>15706.089474</td>\n",
              "      <td>72.3</td>\n",
              "      <td>64.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">sedan</th>\n",
              "      <th>4wd</th>\n",
              "      <td>54.3</td>\n",
              "      <td>54.3</td>\n",
              "      <td>13912.066667</td>\n",
              "      <td>66.4</td>\n",
              "      <td>65.4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>56.1</td>\n",
              "      <td>50.6</td>\n",
              "      <td>10924.022807</td>\n",
              "      <td>71.4</td>\n",
              "      <td>62.5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>56.7</td>\n",
              "      <td>47.8</td>\n",
              "      <td>23883.016667</td>\n",
              "      <td>71.7</td>\n",
              "      <td>61.8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">wagon</th>\n",
              "      <th>4wd</th>\n",
              "      <td>59.1</td>\n",
              "      <td>54.9</td>\n",
              "      <td>10005.325000</td>\n",
              "      <td>65.4</td>\n",
              "      <td>63.6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>fwd</th>\n",
              "      <td>59.8</td>\n",
              "      <td>53.0</td>\n",
              "      <td>10997.066667</td>\n",
              "      <td>71.4</td>\n",
              "      <td>63.6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>rwd</th>\n",
              "      <td>58.7</td>\n",
              "      <td>54.1</td>\n",
              "      <td>18693.644444</td>\n",
              "      <td>70.3</td>\n",
              "      <td>66.5</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                         height               price width      \n",
              "                            max   min          mean   max   min\n",
              "body-style  drive-wheels                                       \n",
              "convertible fwd            55.6  55.6  12754.500000  64.2  64.2\n",
              "            rwd            53.0  48.8  26344.560000  70.5  64.1\n",
              "hardtop     fwd            53.3  53.3   9073.900000  63.8  63.8\n",
              "            rwd            55.4  51.6  26622.985714  72.0  65.0\n",
              "hatchback   4wd            55.7  52.0  11445.500000  67.9  63.8\n",
              "            fwd            56.1  49.4   9236.026531  66.6  60.3\n",
              "            rwd            54.8  49.6  15706.089474  72.3  64.0\n",
              "sedan       4wd            54.3  54.3  13912.066667  66.4  65.4\n",
              "            fwd            56.1  50.6  10924.022807  71.4  62.5\n",
              "            rwd            56.7  47.8  23883.016667  71.7  61.8\n",
              "wagon       4wd            59.1  54.9  10005.325000  65.4  63.6\n",
              "            fwd            59.8  53.0  10997.066667  71.4  63.6\n",
              "            rwd            58.7  54.1  18693.644444  70.3  66.5"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 173
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0VhQ5eh1foF0",
        "colab_type": "text"
      },
      "source": [
        "**Cross-Tabulations**\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dItAnLfie9qT",
        "colab_type": "code",
        "outputId": "6f76f3a6-7dec-48dc-dfe9-fbc075225671",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 762
        }
      },
      "source": [
        "# apply pd.crosstab() function in data frame df\n",
        "pd.crosstab(df[\"make\"], df[\"body-style\"])"
      ],
      "execution_count": 174,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>body-style</th>\n",
              "      <th>convertible</th>\n",
              "      <th>hardtop</th>\n",
              "      <th>hatchback</th>\n",
              "      <th>sedan</th>\n",
              "      <th>wagon</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>make</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>audi</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>bmw</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>chevrolet</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>dodge</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>honda</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>isuzu</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jaguar</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mazda</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>10</td>\n",
              "      <td>7</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercedes-benz</th>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercury</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mitsubishi</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>nissan</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>9</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>peugot</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>plymouth</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>porsche</th>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>renault</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>saab</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>subaru</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>5</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>toyota</th>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>14</td>\n",
              "      <td>10</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volkswagen</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>9</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volvo</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "body-style     convertible  hardtop  hatchback  sedan  wagon\n",
              "make                                                        \n",
              "alfa-romero              2        0          1      0      0\n",
              "audi                     0        0          1      5      1\n",
              "bmw                      0        0          0      8      0\n",
              "chevrolet                0        0          2      1      0\n",
              "dodge                    0        0          5      3      1\n",
              "honda                    0        0          7      5      1\n",
              "isuzu                    0        0          1      3      0\n",
              "jaguar                   0        0          0      3      0\n",
              "mazda                    0        0         10      7      0\n",
              "mercedes-benz            1        2          0      4      1\n",
              "mercury                  0        0          1      0      0\n",
              "mitsubishi               0        0          9      4      0\n",
              "nissan                   0        1          5      9      3\n",
              "peugot                   0        0          0      7      4\n",
              "plymouth                 0        0          4      2      1\n",
              "porsche                  1        2          2      0      0\n",
              "renault                  0        0          1      0      1\n",
              "saab                     0        0          3      3      0\n",
              "subaru                   0        0          3      5      4\n",
              "toyota                   1        3         14     10      4\n",
              "volkswagen               1        0          1      9      1\n",
              "volvo                    0        0          0      8      3"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 174
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7njbPXQTdvE9",
        "colab_type": "code",
        "outputId": "d372dd66-56cd-48ac-ba1e-239acd868a39",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 793
        }
      },
      "source": [
        "# apply margins and margins_name attribute to displays the row wise \n",
        "# and column wise sum of the cross table\n",
        "pd.crosstab(df[\"make\"], df[\"body-style\"],margins=True,margins_name=\"Total Made\")"
      ],
      "execution_count": 175,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>body-style</th>\n",
              "      <th>convertible</th>\n",
              "      <th>hardtop</th>\n",
              "      <th>hatchback</th>\n",
              "      <th>sedan</th>\n",
              "      <th>wagon</th>\n",
              "      <th>Total Made</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>make</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>audi</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>bmw</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>chevrolet</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>dodge</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>3</td>\n",
              "      <td>1</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>honda</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "      <td>13</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>isuzu</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jaguar</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mazda</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>10</td>\n",
              "      <td>7</td>\n",
              "      <td>0</td>\n",
              "      <td>17</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercedes-benz</th>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>1</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercury</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mitsubishi</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>13</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>nissan</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>9</td>\n",
              "      <td>3</td>\n",
              "      <td>18</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>peugot</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>4</td>\n",
              "      <td>11</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>plymouth</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>porsche</th>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>renault</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>saab</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>6</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>subaru</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>5</td>\n",
              "      <td>4</td>\n",
              "      <td>12</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>toyota</th>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>14</td>\n",
              "      <td>10</td>\n",
              "      <td>4</td>\n",
              "      <td>32</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volkswagen</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>9</td>\n",
              "      <td>1</td>\n",
              "      <td>12</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volvo</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "      <td>3</td>\n",
              "      <td>11</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Total Made</th>\n",
              "      <td>6</td>\n",
              "      <td>8</td>\n",
              "      <td>70</td>\n",
              "      <td>96</td>\n",
              "      <td>25</td>\n",
              "      <td>205</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "body-style     convertible  hardtop  hatchback  sedan  wagon  Total Made\n",
              "make                                                                    \n",
              "alfa-romero              2        0          1      0      0           3\n",
              "audi                     0        0          1      5      1           7\n",
              "bmw                      0        0          0      8      0           8\n",
              "chevrolet                0        0          2      1      0           3\n",
              "dodge                    0        0          5      3      1           9\n",
              "honda                    0        0          7      5      1          13\n",
              "isuzu                    0        0          1      3      0           4\n",
              "jaguar                   0        0          0      3      0           3\n",
              "mazda                    0        0         10      7      0          17\n",
              "mercedes-benz            1        2          0      4      1           8\n",
              "mercury                  0        0          1      0      0           1\n",
              "mitsubishi               0        0          9      4      0          13\n",
              "nissan                   0        1          5      9      3          18\n",
              "peugot                   0        0          0      7      4          11\n",
              "plymouth                 0        0          4      2      1           7\n",
              "porsche                  1        2          2      0      0           5\n",
              "renault                  0        0          1      0      1           2\n",
              "saab                     0        0          3      3      0           6\n",
              "subaru                   0        0          3      5      4          12\n",
              "toyota                   1        3         14     10      4          32\n",
              "volkswagen               1        0          1      9      1          12\n",
              "volvo                    0        0          0      8      3          11\n",
              "Total Made               6        8         70     96     25         205"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 175
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7YF-d318yIiW",
        "colab_type": "code",
        "outputId": "de6533ac-04d2-45d8-bcda-cd6ca3115fb0",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        }
      },
      "source": [
        "pd.crosstab([df[\"make\"],df[\"num-of-doors\"]], [df[\"body-style\"],df[\"drive-wheels\"]],\n",
        "            margins=True,margins_name=\"Total Made\")"
      ],
      "execution_count": 176,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr th {\n",
              "        text-align: left;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr:last-of-type th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th>body-style</th>\n",
              "      <th colspan=\"2\" halign=\"left\">convertible</th>\n",
              "      <th colspan=\"2\" halign=\"left\">hardtop</th>\n",
              "      <th colspan=\"3\" halign=\"left\">hatchback</th>\n",
              "      <th colspan=\"3\" halign=\"left\">sedan</th>\n",
              "      <th colspan=\"3\" halign=\"left\">wagon</th>\n",
              "      <th>Total Made</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th>drive-wheels</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>make</th>\n",
              "      <th>num-of-doors</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">audi</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">bmw</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">chevrolet</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">dodge</th>\n",
              "      <th>?</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">honda</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">isuzu</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">jaguar</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">mazda</th>\n",
              "      <th>?</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">mercedes-benz</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercury</th>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">mitsubishi</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">nissan</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>peugot</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "      <td>11</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">plymouth</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>porsche</th>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">renault</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">saab</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">subaru</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>9</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">toyota</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>6</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>1</td>\n",
              "      <td>2</td>\n",
              "      <td>1</td>\n",
              "      <td>1</td>\n",
              "      <td>18</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>6</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>14</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">volkswagen</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>7</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>4</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volvo</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>8</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>11</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Total Made</th>\n",
              "      <th></th>\n",
              "      <td>1</td>\n",
              "      <td>5</td>\n",
              "      <td>1</td>\n",
              "      <td>7</td>\n",
              "      <td>2</td>\n",
              "      <td>49</td>\n",
              "      <td>19</td>\n",
              "      <td>3</td>\n",
              "      <td>57</td>\n",
              "      <td>36</td>\n",
              "      <td>4</td>\n",
              "      <td>12</td>\n",
              "      <td>9</td>\n",
              "      <td>205</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "body-style                 convertible     hardtop      ... wagon         Total Made\n",
              "drive-wheels                       fwd rwd     fwd rwd  ...   4wd fwd rwd           \n",
              "make          num-of-doors                              ...                         \n",
              "alfa-romero   two                    0   2       0   0  ...     0   0   0          3\n",
              "audi          four                   0   0       0   0  ...     0   1   0          5\n",
              "              two                    0   0       0   0  ...     0   0   0          2\n",
              "bmw           four                   0   0       0   0  ...     0   0   0          5\n",
              "              two                    0   0       0   0  ...     0   0   0          3\n",
              "chevrolet     four                   0   0       0   0  ...     0   0   0          1\n",
              "              two                    0   0       0   0  ...     0   0   0          2\n",
              "dodge         ?                      0   0       0   0  ...     0   0   0          1\n",
              "              four                   0   0       0   0  ...     0   1   0          4\n",
              "              two                    0   0       0   0  ...     0   0   0          4\n",
              "honda         four                   0   0       0   0  ...     0   1   0          5\n",
              "              two                    0   0       0   0  ...     0   0   0          8\n",
              "isuzu         four                   0   0       0   0  ...     0   0   0          2\n",
              "              two                    0   0       0   0  ...     0   0   0          2\n",
              "jaguar        four                   0   0       0   0  ...     0   0   0          2\n",
              "              two                    0   0       0   0  ...     0   0   0          1\n",
              "mazda         ?                      0   0       0   0  ...     0   0   0          1\n",
              "              four                   0   0       0   0  ...     0   0   0          7\n",
              "              two                    0   0       0   0  ...     0   0   0          9\n",
              "mercedes-benz four                   0   0       0   0  ...     0   0   1          5\n",
              "              two                    0   1       0   2  ...     0   0   0          3\n",
              "mercury       two                    0   0       0   0  ...     0   0   0          1\n",
              "mitsubishi    four                   0   0       0   0  ...     0   0   0          4\n",
              "              two                    0   0       0   0  ...     0   0   0          9\n",
              "nissan        four                   0   0       0   0  ...     0   3   0          9\n",
              "              two                    0   0       1   0  ...     0   0   0          9\n",
              "peugot        four                   0   0       0   0  ...     0   0   4         11\n",
              "plymouth      four                   0   0       0   0  ...     0   1   0          4\n",
              "              two                    0   0       0   0  ...     0   0   0          3\n",
              "porsche       two                    0   1       0   2  ...     0   0   0          5\n",
              "renault       four                   0   0       0   0  ...     0   1   0          1\n",
              "              two                    0   0       0   0  ...     0   0   0          1\n",
              "saab          four                   0   0       0   0  ...     0   0   0          3\n",
              "              two                    0   0       0   0  ...     0   0   0          3\n",
              "subaru        four                   0   0       0   0  ...     2   2   0          9\n",
              "              two                    0   0       0   0  ...     0   0   0          3\n",
              "toyota        four                   0   0       0   0  ...     2   1   1         18\n",
              "              two                    0   1       0   3  ...     0   0   0         14\n",
              "volkswagen    four                   0   0       0   0  ...     0   1   0          8\n",
              "              two                    1   0       0   0  ...     0   0   0          4\n",
              "volvo         four                   0   0       0   0  ...     0   0   3         11\n",
              "Total Made                           1   5       1   7  ...     4  12   9        205\n",
              "\n",
              "[42 rows x 14 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 176
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jXHG_3-805q8",
        "colab_type": "code",
        "outputId": "1eefb59e-fdf2-4599-9811-9af522aa7272",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 266
        }
      },
      "source": [
        "# rename the columns and row index for better understanding of crosstab\n",
        "pd.crosstab([df[\"make\"],df[\"num-of-doors\"]], [df[\"body-style\"],df[\"drive-wheels\"]],\n",
        "            rownames=['Auto Manufacturer', \"Doors\"],\n",
        "            colnames=['Body Style', \"Drive Type\"],\n",
        "            margins=True,margins_name=\"Total Made\").head()\n",
        "\n"
      ],
      "execution_count": 177,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr th {\n",
              "        text-align: left;\n",
              "    }\n",
              "\n",
              "    .dataframe thead tr:last-of-type th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th>Body Style</th>\n",
              "      <th colspan=\"2\" halign=\"left\">convertible</th>\n",
              "      <th colspan=\"2\" halign=\"left\">hardtop</th>\n",
              "      <th colspan=\"3\" halign=\"left\">hatchback</th>\n",
              "      <th colspan=\"3\" halign=\"left\">sedan</th>\n",
              "      <th colspan=\"3\" halign=\"left\">wagon</th>\n",
              "      <th>Total Made</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th></th>\n",
              "      <th>Drive Type</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th>4wd</th>\n",
              "      <th>fwd</th>\n",
              "      <th>rwd</th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Auto Manufacturer</th>\n",
              "      <th>Doors</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">audi</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"2\" valign=\"top\">bmw</th>\n",
              "      <th>four</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>two</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "Body Style              convertible     hardtop      ... wagon         Total Made\n",
              "Drive Type                      fwd rwd     fwd rwd  ...   4wd fwd rwd           \n",
              "Auto Manufacturer Doors                              ...                         \n",
              "alfa-romero       two             0   2       0   0  ...     0   0   0          3\n",
              "audi              four            0   0       0   0  ...     0   1   0          5\n",
              "                  two             0   0       0   0  ...     0   0   0          2\n",
              "bmw               four            0   0       0   0  ...     0   0   0          5\n",
              "                  two             0   0       0   0  ...     0   0   0          3\n",
              "\n",
              "[5 rows x 14 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 177
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vxhNBPbE1ZQ6",
        "colab_type": "code",
        "outputId": "678a7355-2ff2-4722-df7c-7ae28c66111b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 762
        }
      },
      "source": [
        "# values are the column in which aggregration function is to be applied\n",
        "# aggfunc is the aggregration function to be applied\n",
        "# round() to round the output\n",
        "\n",
        "pd.crosstab(df[\"make\"], df[\"body-style\"],values=df[\"curb-weight\"],\n",
        "            aggfunc='mean').round(0)"
      ],
      "execution_count": 178,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>body-style</th>\n",
              "      <th>convertible</th>\n",
              "      <th>hardtop</th>\n",
              "      <th>hatchback</th>\n",
              "      <th>sedan</th>\n",
              "      <th>wagon</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>make</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <td>2548.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2823.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>audi</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>3053.0</td>\n",
              "      <td>2720.0</td>\n",
              "      <td>2954.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>bmw</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2929.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>chevrolet</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>1681.0</td>\n",
              "      <td>1909.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>dodge</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2132.0</td>\n",
              "      <td>2056.0</td>\n",
              "      <td>2535.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>honda</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>1970.0</td>\n",
              "      <td>2289.0</td>\n",
              "      <td>2024.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>isuzu</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2734.0</td>\n",
              "      <td>2040.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jaguar</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>4027.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mazda</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2254.0</td>\n",
              "      <td>2361.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercedes-benz</th>\n",
              "      <td>3685.0</td>\n",
              "      <td>3605.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>3731.0</td>\n",
              "      <td>3750.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercury</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2910.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mitsubishi</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2377.0</td>\n",
              "      <td>2394.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>nissan</th>\n",
              "      <td>NaN</td>\n",
              "      <td>2008.0</td>\n",
              "      <td>2740.0</td>\n",
              "      <td>2238.0</td>\n",
              "      <td>2452.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>peugot</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>3143.0</td>\n",
              "      <td>3358.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>plymouth</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2208.0</td>\n",
              "      <td>2090.0</td>\n",
              "      <td>2535.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>porsche</th>\n",
              "      <td>2800.0</td>\n",
              "      <td>2756.0</td>\n",
              "      <td>3072.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>renault</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2460.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2579.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>saab</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2724.0</td>\n",
              "      <td>2767.0</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>subaru</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2137.0</td>\n",
              "      <td>2314.0</td>\n",
              "      <td>2454.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>toyota</th>\n",
              "      <td>2975.0</td>\n",
              "      <td>2585.0</td>\n",
              "      <td>2370.0</td>\n",
              "      <td>2338.0</td>\n",
              "      <td>2708.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volkswagen</th>\n",
              "      <td>2254.0</td>\n",
              "      <td>NaN</td>\n",
              "      <td>2221.0</td>\n",
              "      <td>2342.0</td>\n",
              "      <td>2563.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>volvo</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>3023.0</td>\n",
              "      <td>3078.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "body-style     convertible  hardtop  hatchback   sedan   wagon\n",
              "make                                                          \n",
              "alfa-romero         2548.0      NaN     2823.0     NaN     NaN\n",
              "audi                   NaN      NaN     3053.0  2720.0  2954.0\n",
              "bmw                    NaN      NaN        NaN  2929.0     NaN\n",
              "chevrolet              NaN      NaN     1681.0  1909.0     NaN\n",
              "dodge                  NaN      NaN     2132.0  2056.0  2535.0\n",
              "honda                  NaN      NaN     1970.0  2289.0  2024.0\n",
              "isuzu                  NaN      NaN     2734.0  2040.0     NaN\n",
              "jaguar                 NaN      NaN        NaN  4027.0     NaN\n",
              "mazda                  NaN      NaN     2254.0  2361.0     NaN\n",
              "mercedes-benz       3685.0   3605.0        NaN  3731.0  3750.0\n",
              "mercury                NaN      NaN     2910.0     NaN     NaN\n",
              "mitsubishi             NaN      NaN     2377.0  2394.0     NaN\n",
              "nissan                 NaN   2008.0     2740.0  2238.0  2452.0\n",
              "peugot                 NaN      NaN        NaN  3143.0  3358.0\n",
              "plymouth               NaN      NaN     2208.0  2090.0  2535.0\n",
              "porsche             2800.0   2756.0     3072.0     NaN     NaN\n",
              "renault                NaN      NaN     2460.0     NaN  2579.0\n",
              "saab                   NaN      NaN     2724.0  2767.0     NaN\n",
              "subaru                 NaN      NaN     2137.0  2314.0  2454.0\n",
              "toyota              2975.0   2585.0     2370.0  2338.0  2708.0\n",
              "volkswagen          2254.0      NaN     2221.0  2342.0  2563.0\n",
              "volvo                  NaN      NaN        NaN  3023.0  3078.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 178
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xlGnv-_Y7pNJ",
        "colab_type": "code",
        "outputId": "a9207ef6-94a3-48ad-e743-7f98c323faf6",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 390
        }
      },
      "source": [
        "# top ten output that represents the percentage of occurrence of the combination \n",
        "pd.crosstab(df[\"make\"], df[\"body-style\"],normalize=True).head(10)"
      ],
      "execution_count": 179,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>body-style</th>\n",
              "      <th>convertible</th>\n",
              "      <th>hardtop</th>\n",
              "      <th>hatchback</th>\n",
              "      <th>sedan</th>\n",
              "      <th>wagon</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>make</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>alfa-romero</th>\n",
              "      <td>0.009756</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.004878</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>audi</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.004878</td>\n",
              "      <td>0.024390</td>\n",
              "      <td>0.004878</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>bmw</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.039024</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>chevrolet</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.009756</td>\n",
              "      <td>0.004878</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>dodge</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.024390</td>\n",
              "      <td>0.014634</td>\n",
              "      <td>0.004878</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>honda</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.034146</td>\n",
              "      <td>0.024390</td>\n",
              "      <td>0.004878</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>isuzu</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.004878</td>\n",
              "      <td>0.014634</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jaguar</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.014634</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mazda</th>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.048780</td>\n",
              "      <td>0.034146</td>\n",
              "      <td>0.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mercedes-benz</th>\n",
              "      <td>0.004878</td>\n",
              "      <td>0.009756</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.019512</td>\n",
              "      <td>0.004878</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "body-style     convertible   hardtop  hatchback     sedan     wagon\n",
              "make                                                               \n",
              "alfa-romero       0.009756  0.000000   0.004878  0.000000  0.000000\n",
              "audi              0.000000  0.000000   0.004878  0.024390  0.004878\n",
              "bmw               0.000000  0.000000   0.000000  0.039024  0.000000\n",
              "chevrolet         0.000000  0.000000   0.009756  0.004878  0.000000\n",
              "dodge             0.000000  0.000000   0.024390  0.014634  0.004878\n",
              "honda             0.000000  0.000000   0.034146  0.024390  0.004878\n",
              "isuzu             0.000000  0.000000   0.004878  0.014634  0.000000\n",
              "jaguar            0.000000  0.000000   0.000000  0.014634  0.000000\n",
              "mazda             0.000000  0.000000   0.048780  0.034146  0.000000\n",
              "mercedes-benz     0.004878  0.009756   0.000000  0.019512  0.004878"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 179
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Mbq_9cR09N2m",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}